package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.MyImageLoader;
import cn.wit.summit.chwan.widgets.ItemListView;

public class DiscoveryChoiceListAdapter extends RecyclerView.Adapter {
    private List<ViewBean> datas;
    Activity context;

    public DiscoveryChoiceListAdapter(Activity context, List<ViewBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    public static class ViewBean {
        int    viewType;
        Object bean;

        public ViewBean(int viewType, Object bean) {
            this.viewType = viewType;
            this.bean = bean;
        }

        public int getViewType() {
            return viewType;
        }

        public void setViewType(int viewType) {
            this.viewType = viewType;
        }

        public Object getBean() {
            return bean;
        }

        public void setBean(Object bean) {
            this.bean = bean;
        }
    }

    public enum ViewType {
        Title,
        AdPic,
        GameItem,
        ChoicePic
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == ViewType.Title.ordinal()) {
            holder = new TitleVH(LayoutInflater.from(context).inflate(R.layout.find_fragment_title_item, parent, false));
        } else if (viewType == ViewType.GameItem.ordinal()) {
            holder = new GameItemVH(LayoutInflater.from(context).inflate(R.layout.find_fragment_game_item_item, parent, false));
        } else if (viewType == ViewType.AdPic.ordinal()) {
            holder = new BillboardVH(LayoutInflater.from(context).inflate(R.layout.find_fragment_billboard_item, parent, false));
        } else if (viewType == ViewType.ChoicePic.ordinal()) {
            holder = new ChoicePicVH(LayoutInflater.from(context).inflate(R.layout.item_choice_pic, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == ViewType.Title.ordinal()) {
            onBindTitleViewHolder((TitleVH) holder, position);
        } else if (viewType == ViewType.GameItem.ordinal()) {
            onBindGameItemViewHolder((GameItemVH) holder, position);
        } else if (viewType == ViewType.AdPic.ordinal()) {
            onBindBillBoardViewHolder((BillboardVH) holder, position);
        } else if (viewType == ViewType.ChoicePic.ordinal()) {
            onBindChoicePicViewHolder((ChoicePicVH) holder, position);
        }
    }

    private void onBindBillBoardViewHolder(BillboardVH holder, int position) {
        final JumpBaseBean jumpBaseBean = (JumpBaseBean) getItem(position);
        MyImageLoader.load(holder.img, jumpBaseBean.getJump_cover_big_pic());

        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.getInstance().intentTo(context, jumpBaseBean);
            }
        });
    }

    private void onBindChoicePicViewHolder(ChoicePicVH holder, int position) {
        final JumpBaseBean jumpBaseBean = (JumpBaseBean) getItem(position);
        MyImageLoader.load(holder.img, jumpBaseBean.getJump_cover_big_pic(), MyImageLoader.makeRoundParams(context, context.getResources().getDimensionPixelOffset(R.dimen.wdp10)));

        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.getInstance().intentTo(context, jumpBaseBean);
            }
        });
    }

    private void onBindGameItemViewHolder(GameItemVH holder, int position) {
        GameListBaseBean gameListBaseBean = (GameListBaseBean) getItem(position);
        holder.listItem.setData(context, gameListBaseBean);

        downTemp.put(gameListBaseBean.getGame_id(), position + "");
    }

    private void onBindTitleViewHolder(TitleVH holder, int position) {
        final JumpBaseBean jumpBaseBean = (JumpBaseBean) getItem(position);
        if (jumpBaseBean == null) return;
        holder.title.setText(jumpBaseBean.getJump_sub_title());
        if (!TextUtils.isEmpty(jumpBaseBean.getJump_label()))
            holder.moreText.setText(jumpBaseBean.getJump_label());
        else holder.moreText.setText("全部");
        if (jumpBaseBean.getJump_info() == null) {
            holder.more.setVisibility(View.GONE);
        } else {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtil.getInstance().intentTo(context, jumpBaseBean);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (datas == null)
            return super.getItemViewType(position);
        else {
            return datas.get(position).getViewType();
        }
    }

    Object getItem(int position) {
        if (datas == null) return null;
        return datas.get(position).getBean();
    }

    @Override
    public int getItemCount() {
        if (datas == null) return 0;
        return datas.size();
    }

    private class TitleVH extends RecyclerView.ViewHolder {
        TextView title;
        TextView moreText;
        View     more;

        public TitleVH(View inflate) {
            super(inflate);
            title = inflate.findViewById(R.id.title);
            moreText = inflate.findViewById(R.id.moreText);
            more = inflate.findViewById(R.id.more);
            if (more.getVisibility() == View.GONE) {
                more.setVisibility(View.VISIBLE);
            }
        }
    }

    private class GameItemVH extends RecyclerView.ViewHolder {
        public ItemListView listItem;

        public GameItemVH(View inflate) {
            super(inflate);
            listItem = inflate.findViewById(R.id.listItem);
        }
    }

    private class BillboardVH extends RecyclerView.ViewHolder {
        public SimpleDraweeView img;

        public BillboardVH(View inflate) {
            super(inflate);
            img = inflate.findViewById(R.id.img);
        }
    }

    private class ChoicePicVH extends RecyclerView.ViewHolder {
        public SimpleDraweeView img;

        public ChoicePicVH(View inflate) {
            super(inflate);
            img = inflate.findViewById(R.id.img);
        }
    }

    //region Download Game
    private Map<String, String> downTemp = new HashMap<>();

    public void clearDownloadStatus() {
        downTemp.clear();
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size() > 0 && downTemp.containsKey(gameId)) {
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (getItemViewType(itemPo) == ViewType.GameItem.ordinal()) {
            GameListBaseBean list = (GameListBaseBean) getItem(itemPo);
            return list;
        }
        return null;
    }
    //endregion

}
